#!/usr/bin/env ruby
# frozen_string_literal: true

# filter-destroy-capstone: find results that only capstone gets right (or wrong)

require "json"

# TODO(ww): Remove this.
CAPSTONE_SO = "./src/worker/capstone/capstone.so"
XED_SO = "./src/worker/xed/xed.so"
ZYDIS_SO = "./src/worker/zydis/zydis.so"

STDERR.puts "[+] pass: filter-destroy-capstone"

count = 0
STDIN.each_line do |line|
  result = JSON.parse line, symbolize_names: true

  capstone = result[:outputs].find { |o| o[:worker_so] == CAPSTONE_SO }
  xed = result[:outputs].find { |o| o[:worker_so] == XED_SO }
  zydis = result[:outputs].find { |o| o[:worker_so] == ZYDIS_SO }

  if capstone[:status][:value] == xed[:status][:value] && capstone[:status] == zydis[:status][:value]
    count += 1
    next
  end

  STDOUT.puts result.to_json
end

STDERR.puts "[+] pass: filter-destroy-capstone done: #{count} filtered"
